package Entity;

import java.sql.ResultSet;
import java.util.ArrayList;

import Control.MySQLController;

public class Workshop_TrainerEntity {
	//Attributes
	private String trainerId;
	private String trainerName;
	private int contactNo;
	
	//Getters and Setters
	public String getTrainerId() {
		return trainerId;
	}
	public void setTrainerId(String trainerId) {
		this.trainerId = trainerId;
	}
	public String getTrainerName() {
		return trainerName;
	}
	public void setTrainerName(String trainerName) {
		this.trainerName = trainerName;
	}
	public int getContactNo() {
		return contactNo;
	}
	public void setContactNo(int contactNo) {
		this.contactNo = contactNo;
	}
	
	//Constructors
	public Workshop_TrainerEntity(){}
	
	public Workshop_TrainerEntity(String trainerId){
		this.trainerId = trainerId;
	}
	
	public Workshop_TrainerEntity(String trainerId, String trainerName, int contactNo){
		this.trainerId = trainerId;
		this.trainerName = trainerName;
		this.contactNo = contactNo;
	}	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	public boolean createTrainer(){
		boolean success = false;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "INSERT INTO ooadpj.trainer(trainer_id, name, contact_no)";
		sql += "VALUES ('" + trainerId + "','" + trainerName + "','" + contactNo + "')";
		if(db.updateRequest(sql)==1)
			success = true;
		db.terminate();
		return success;
	}
	
	public static ArrayList<Workshop_TrainerEntity> retrieveAllTrainers(){
		ArrayList<Workshop_TrainerEntity> trainerList = new ArrayList<Workshop_TrainerEntity>();
		
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.trainer";
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				Workshop_TrainerEntity trainer = new Workshop_TrainerEntity();
				trainer.setTrainerId(rs.getString("trainer_id"));
				trainer.setTrainerName(rs.getString("name"));
				trainer.setContactNo(rs.getInt("contact_no"));
				trainerList.add(trainer);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return trainerList;
	}
	
	public boolean retrieveATrainer(){
		boolean success = false;
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.trainer WHERE trainer_id = '" + trainerId + "'";
		rs = db.readRequest(sql);
		try{
			while(rs.next()){
				trainerName = rs.getString("name");
				contactNo = rs.getInt("contact_no");
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return success;
	}
	
	public boolean updateTrainerContact(){
		boolean success = false;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "UPDATE ooadpj.trainer SET contact_no = '" + contactNo + "' WHERE trainer_id = '" + trainerId + "'";
		if(db.updateRequest(sql)==1)
			success = true;
		db.terminate();
		return success;
	}
	
	public boolean deleteTrainer(){
		boolean success = false;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "DELETE FROM ooadpj.trainer WHERE trainer_id = '" + trainerId + "'";
		if(db.updateRequest(sql)==1)
			success = true;
		db.terminate();
		return success;
	}
}
